//
//  SXPWellcomeViewController.swift
//  weibo
//
//  Created by shixinPeng on 16/2/27.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class SXPWellcomeViewController: UIViewController {

    //snapKit的约束属性
    var snapKitBottomConstaint: Constraint?
    
    //更换背景图片
    override func loadView() {
        view = bigImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加控件
        setUpUI()
        
     
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidDisappear(animated)
        
        startAnmotion()
    }
    
    //图层动画
    private func startAnmotion(){
        
        snapKitBottomConstaint?.updateOffset(100)
        
//        UIView.animateWithDuration(1, animations: { () -> Void in
//            
//            //刷新图层
//            self.view.layoutIfNeeded()
//            
//            
//            }) { _ -> Void in
//                //跳转控制器
//        }
        UIView.animateWithDuration(3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: []
            , animations: { () -> Void in
                
                self.view.layoutIfNeeded()
                
            }) { _ -> Void in
                
                //print("跳转控制器")
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangRootVC, object: nil)
        }
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //2:创建一个自定义方法,更新UI和添加约束
    private func setUpUI() {
        //添加控件
        view.addSubview(userIconImageView)
        view.addSubview(nameLabel)
        
        
        //添加头像约束
        userIconImageView.snp_makeConstraints { (make) -> Void in
             make.centerX.equalTo(view.snp_centerX)
            snapKitBottomConstaint = make.top.equalTo(300).constraint
            make.size.equalTo(CGSizeMake(170*0.5, 170*0.5))
            
        }
        //添加标签约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(userIconImageView.snp_bottom).offset(15)
            
        }
        
        let url = NSURL(string:SXPuserViewModels.shareInstance.userModel?.profile_image_url ?? "")
        
        if let Ourl = url {
            
            //print(Ourl)
            
            userIconImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        

        

    }
    //3.懒加载
    //背景图
    lazy var bigImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    //头像
    lazy var userIconImageView:UIImageView = {
       let img = UIImageView()
        //设置预设图片
        img.image = UIImage(named: "avatar_default_big")
        //做圆角
        img.layer.cornerRadius = 170*0.25
        //做边框
        img.layer.borderWidth = 2
        img.layer.borderColor = UIColor.lightGrayColor().CGColor
        //切圆角
        //img.clipsToBounds = true
        img.layer.masksToBounds = true
        
        //设置图片大小
        //img.sizeToFit()
        
        return img
        
        
    }()
    
    //标签
    lazy var nameLabel:UILabel = {
       let lb = UILabel()
        
        lb.text = "欢迎主人"
        
        lb.textColor = UIColor.darkGrayColor()
        
        return lb
        
    }()
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
